<?php
/**
 * 图标管理
 * Created by gii.
 * User: Gii
 * Date: 2022/03/18
 * Time: 16:49
 */

namespace app\admin\model\model\system;

use app\admin\model\traits\Common;
use think\Model;

class SystemAccount extends Model
{
	use Common;

    const USER_SUPER = 1; // 超级管理员ID

    const STATUS_ENABLE = 1; // 启用
    const STATUS_DISABLE = 0; // 禁用

    public static function cleanToken($id) {
        $where = [
            'id'    => $id,
        ];
        $data = [
            'token' => '',
        ];
        return self::updateCommon($data, $where);
    }

    /**
     * 根据token获取用户信息
     * @param $token
     * @return Common|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getByToken($token) {
        $where = [
            'status'    => self::STATUS_ENABLE,
            'token'     => $token,
        ];
        return self::getLineCommon($where);
    }

    /**
     * 根据用户名获取用户,用于表单验证
     * @param $username
     * @return self|Common|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getByUsername($username) {
        $where = [
            'status' => self::STATUS_ENABLE,
            'username' => $username,
        ];
        return self::getLineCommon($where);
    }

    /**
     * 修改最后登录时间
     * @param $id
     * @return Common
     */
    public static function changeLastLogin($id, $token='') {
        $data = [
            'id' => $id,
            'lastlogin' => time(),
        ];
        if($token) {
            $data['token'] = $token;
        }
        return self::updateCommon($data);
    }

    /**
     * 修改管理员状态
     * @param $id
     * @param $status
     * @return Common
     */
    public static function status($id, $status) {
        $data = [
            'id' => $id,
            'status' => $status,
        ];
        return self::updateCommon($data);
    }

    /**
     * 获取where条件
     * @param array $data 参数数组
     * @param array $fields 要处理的字段
     * @return array
     */
    public static function getSelfWhereCommon(Array $data , $fields = []) {
        $keys = array_keys($data);
        if($fields) {
            //去掉data里面fields没指定的字段
            $keys = array_intersect($keys , $fields);
        }
        $where = [];
        foreach($keys as $key) {
            $val = $data[$key] ?? '';
            if($val === '') continue; //过滤空值
            switch ($key) {
                case 'username':
                    $where[] = [$key, 'like', "%" .$val. "%"];
                    break;
                default:
                    $where[] = [$key, '=', $val];
            }
        }
        return $where;
    }

    public function getPassword() {
        return $this->getData('password');
    }

    public function getId() {
        return $this->getData('id');
    }

    public function getLastloginAttr($val) {
        return date('Y-m-d H:i:s', $val);
    }

    public function getToken() {
        return $this->getData('token');
    }
}
